package fhp.hlhj.giantfold.adapter

import android.content.Context
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import de.hdodenhof.circleimageview.CircleImageView
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.javaBean.MerchantBean
import fhp.hlhj.giantfold.javaBean.SellersBean
import fhp.hlhj.giantfold.network.Urls
import fhp.hlhj.giantfold.utils.LogUtil
import fhp.hlhj.giantfold.utils.MyUtils
import java.util.*

/**
 * author: FHP
 * data: on 2017\11\23 0023 10:01
 * E-Mail: 103945089@qq.com
 */
class MerchantAdp(private var context: Context, private var datas: ArrayList<MerchantBean.DataBean>) :BaseAdapter() {

    override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
        var p1 =p1
        if (p1==null){
            p1=LayoutInflater.from(context).inflate(R.layout.movie_list_item,null)
        }
        val itemImg = p1?.findViewOften<CircleImageView>(R.id.itemImg)
        val tvTittle = p1?.findViewById<TextView>(R.id.tvTittle)
        val tvDay = p1?.findViewOften<TextView>(R.id.tvDay)
        val tvH = p1?.findViewOften<TextView>(R.id.tvH)
        val tvMin = p1?.findViewOften<TextView>(R.id.tvMin)
        val tvSec = p1?.findViewOften<TextView>(R.id.tvSec)

        val bean = datas[p0]
        tvTittle?.text = bean.name
        Glide.with(context).load(Urls.BaseUrl+ bean.savepath0+ bean.savename0).asBitmap().placeholder(R.mipmap.exp1).into(itemImg)

        val timer = Timer()
        val task = object:TimerTask(){
            override fun run() {
                tvTittle?.post(Runnable {
                    val endTimestamp = MyUtils.dateToStamp(bean.end_time)
                    val time = (endTimestamp.toLong() - System.currentTimeMillis())/1000
                    val day=time/60/60/24
                    val hour = time / (60 * 60)-day*24
                    val min = time % 3600 /60
                    var sec=time%3600% 60
                    tvDay?.text="距离结束还有${day}天"
                    tvH?.text=hour.toString()
                    tvMin?.text=min.toString()
                    tvSec?.text=sec.toString()
                })
            }
        }
        timer.schedule(task,0,1000)
        return p1!!

    }

    override fun getItem(p0: Int): Any {
        return datas[p0]
    }

    override fun getItemId(p0: Int): Long {
        return p0.toLong()
    }

    override fun getCount(): Int {
        return datas.size
    }
    fun <T : View> View.findViewOften(viewId: Int): T {
        var viewHolder: SparseArray<View> = tag as? SparseArray<View> ?: SparseArray()
        tag = viewHolder
        var childView: View? = viewHolder.get(viewId)
        if (null == childView) {
            childView = findViewById(viewId)
            viewHolder.put(viewId, childView)
        }
        return childView as T
    }


}